package work.vcloud.baseinfo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import work.vcloud.frame.controller.BaseController;
import work.vcloud.frame.dao.OpenDao;
import work.vcloud.frame.vo.OutputVo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created with IntelliJ IDEA.
 *
 * @Version: 1.0
 * @ClassName： SchoolRollController
 * @Description：
 * @Author： EvanZhao
 * @DateTime： 2022-08-10 9:51
 */

@RestController
@Scope("prototype")
public class StudentChangeController extends BaseController {

    @Autowired
    OpenDao openDao;

    @RequestMapping("api/saveTransferInfo")
    public OutputVo saveTransferInfo() {
        try {
            Map<String, Object> map = getParameterMap();
            map.remove("page");

            if ("1".equals(map.get("type").toString())){
                //转入
                //生成学籍
                map.put("sqlMapId", "student.insert");
                map.put("fk_student", openDao.insert(map));

                //保存转学信息
                map.put("id_card", map.get("sfzjh").toString());
                map.put("fk_state", map.get("real_state").toString());
                map.put("sqlMapId", "student_change.insert");
                openDao.insert(map);
                return output("0000", "", map.get("fk_student").toString());
            }else {
                //逻辑删除现有学生变更信息
                Map<String, Object> delObj = new HashMap<>();
                delObj.put("fk_unit", map.get("fk_unit").toString());
                delObj.put("fk_student", map.get("fk_student").toString());
                delObj.put("sqlMapId", "student_change.delete");
                openDao.update(delObj);

                //保存学生变更信息
                map.put("id_card", map.get("sfzjh").toString());
                if ("2".equals(map.get("type").toString()) || "3".equals(map.get("type").toString())){
                    //转出或休学
                    map.put("fk_state", map.get("state").toString());
                }else{
                    map.put("fk_state", map.get("real_state").toString());
                }
                map.put("sqlMapId", "student_change.insert");
                openDao.insert(map);

                //修改学籍信息
                map.put("sqlMapId", "student.updateState");
                openDao.update(map);
                return output("0000");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return output("9999");
        }
    }

    @RequestMapping("api/cancelStudentChange")
    public OutputVo cancelStudentChange() {
        try {
            Map<String, Object> map = getParameterMap();
            map.remove("page");

            //撤销学生变更
            Map<String, Object> backoutObj = new HashMap<>();
            backoutObj.put("id", map.get("id").toString());
            backoutObj.put("is_cancel", "1");
            backoutObj.put("sqlMapId", "student_change.update");
            openDao.update(backoutObj);
            //map.put("sqlMapId", "student_change.exchangeState");
            //openDao.update(map);

            if ("1".equals(map.get("type").toString())){
                //真删除学籍信息
                map.put("sqlMapId", "student.delStudent");
                openDao.delete(map);
            }else{
                //修改学籍信息
                map.put("sqlMapId", "student.updateState");
                openDao.update(map);
            }
            return output("0000");
        } catch (Exception e) {
            e.printStackTrace();
            return output("9999");
        }
    }

    @RequestMapping("api/upgrade")
    public OutputVo upgrade() {
        try {
            Map<String, Object> map = getParameterMap();
            map.remove("page");

            //查询所有年级
            map.put("sqlMapId", "student_change.getGrade");
            List<Map<String, Object>> gradeList = openDao.queryForList(map);
            int gradeCount = gradeList.size();
            if (gradeCount>0){
                for (Map<String, Object> grade : gradeList) {
                    map.put("sort", String.valueOf(Integer.valueOf(grade.get("sort").toString())+1));
                    map.put("sqlMapId", "student_change.getGrade");
                    List<Map<String, Object>> nextGradeList = openDao.queryForList(map);
                    // nextGrade: 下一年级
                    Map<String, Object> nextGrade = nextGradeList.size()>0?nextGradeList.get(0):new HashMap<>();
                    // 查询当前年级的所有班级
                    map.remove("sort");
                    map.put("fk_grade", grade.get("id").toString());
                    map.put("sqlMapId", "student_change.getClassByGrade");
                    List<Map<String, Object>> classList = openDao.queryForList(map);
                    int classCount = classList.size();
                    if (classCount>0){
                        for (Map<String, Object> iClass : classList) {
                            map.put("old_fk_grade", grade.get("id").toString());
                            map.put("old_fk_class", iClass.get("id").toString());
                            if (nextGrade.isEmpty()){
                                //升学
                                map.put("new_fk_grade", "");
                                map.put("new_fk_class", "");
                                map.put("is_graduate", "1");
                            }else{
                                map.put("fk_grade", nextGrade.get("id").toString());
                                map.put("sort", iClass.get("sort").toString());
                                map.put("sqlMapId", "student_change.getClassByGrade");
                                List<Map<String, Object>> nextClassList = openDao.queryForList(map);
                                // nextClass: 下一班级
                                Map<String, Object> nextClass = nextClassList.size()>0?nextClassList.get(0):new HashMap<>();
                                String nextClassId = "";
                                if (nextClass.isEmpty()){
                                    //新增班级
                                    Map<String, Object> addObj = new HashMap<>();
                                    addObj.put("userInfo", map.get("userInfo"));
                                    addObj.put("fk_unit", map.get("fk_unit").toString());
                                    addObj.put("class_name", nextGrade.get("name").toString()+"("+iClass.get("sort").toString()+")班");
                                    addObj.put("new_fk_grade", nextGrade.get("id").toString());
                                    addObj.put("old_fk_grade", grade.get("id").toString());
                                    addObj.put("old_fk_class", iClass.get("id").toString());
                                    addObj.put("sqlMapId", "student_change.addClass");
                                    nextClassId = openDao.insert(addObj).toString();
                                }
                                // 升级
                                map.put("new_fk_grade", nextGradeList.get(0).get("id").toString());
                                map.put("new_fk_class", nextClass.isEmpty()?nextClassId:nextClassList.get(0).get("id").toString());
                                map.put("is_graduate", "0");
                            }
                            map.put("sqlMapId", "student_change.upgradeClass");
                            openDao.insert(map);
                        }
                    }

                }
            } else {
                return output("4000", "", "无数据可供操作！");
            }
            return output("0000");
        } catch (Exception e) {
            e.printStackTrace();
            return output("9999");
        }
    }

    @RequestMapping("api/updateUpgradeState")
    public OutputVo updateUpgradeState() {
        try {
            Map<String, Object> map = getParameterMap();
            map.remove("page");

            map.put("sqlMapId", "student_upgrade.findUpgradeStudent");
            List<Map<String, Object>> studentList = openDao.queryForList(map);
            for (Map<String, Object> student : studentList) {
                Map<String, Object> updateObj = new HashMap<>();
                updateObj.put("fk_unit", student.get("fk_unit").toString());
                updateObj.put("id", student.get("fk_student").toString());
                if ("0".equals(map.get("is_roll_back").toString())){
                    updateObj.put("fk_grade", student.get("new_fk_grade").toString());
                    updateObj.put("fk_class", student.get("new_fk_class").toString());
                    if ("0".equals(student.get("is_graduate").toString())){
                        updateObj.put("state", student.get("fk_state")==null?"":student.get("fk_state").toString());
                    }else {
                        // 1
                        Map<String, Object> query = new HashMap<>();
                        query.put("name", "毕业");
                        query.put("fk_rule", "SJAAAAA10061");
                        query.put("sqlMapId", "dictionary.selectByRule");
                        List<Map<String, Object>> stateList = openDao.queryForList(query);
                        updateObj.put("state", stateList.get(0).get("id").toString());
                    }
                }else{
                    updateObj.put("fk_grade", student.get("old_fk_grade").toString());
                    updateObj.put("fk_class", student.get("old_fk_class").toString());
                    updateObj.put("state", student.get("fk_state").toString());
                    //删除
                    Map<String, Object> delObj = new HashMap<>();
                    delObj.put("id", student.get("id").toString());
                    delObj.put("is_cancel", "1");
                    delObj.put("delete_flag", 1);
                    delObj.put("sqlMapId", "student_upgrade.batchChangeState");
                    openDao.update(delObj);
                }
                updateObj.put("userInfo", map.get("userInfo"));
                updateObj.put("sqlMapId", "student_upgrade.updateUpgradeState");
                openDao.update(updateObj);
            }
            return output("0000");
        } catch (Exception e) {
            e.printStackTrace();
            return output("9999");
        }
    }

    /**
     * 批量升学确认
     */
    @RequestMapping("api/batchConfirmEnrollment")
    public OutputVo batchConfirmEnrollment(){
        try {
            Map<String, Object> map = getParameterMap();
            map.remove("page");
            // 查询待升学确认学生列表
            map.put("is_affirm", "0");
            map.put("sqlMapId", "student_upgrade.findUpgradeStudent");
            List<Map<String, Object>> studentList = openDao.queryForList(map);
            // 批量升学确认
            map.put("sqlMapId", "student_upgrade.batchConfirmState");
            openDao.update(map);
            // 修改基础学生信息状态-升学
            for (Map<String, Object> student : studentList) {
                Map<String, Object> updateObj = new HashMap<>();
                updateObj.put("fk_unit", student.get("fk_unit").toString());
                updateObj.put("id", student.get("fk_student").toString());
                updateObj.put("fk_grade", student.get("new_fk_grade").toString());
                updateObj.put("fk_class", student.get("new_fk_class").toString());
                if ("0".equals(student.get("is_graduate").toString())){
                    updateObj.put("state", student.get("fk_state")==null?"":student.get("fk_state").toString());
                }else {
                    // 1：毕业
                    Map<String, Object> query = new HashMap<>();
                    query.put("name", "毕业");
                    query.put("fk_rule", "SJAAAAA10061");
                    query.put("sqlMapId", "dictionary.selectByRule");
                    List<Map<String, Object>> stateList = openDao.queryForList(query);
                    updateObj.put("state", stateList.get(0).get("id").toString());
                }
                updateObj.put("userInfo", map.get("userInfo"));
                updateObj.put("sqlMapId", "student_upgrade.updateUpgradeState");
                openDao.update(updateObj);
            }
            return output("0000");
        } catch (Exception e){
            e.printStackTrace();
            return output("9999");
        }
    }
}
